package org.javaforever.cookieshop.controller;

import java.util.Map;
import java.util.TreeMap;

import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.domain.PageBean;
import org.javaforever.cookieshop.domain.UserBean;
import org.javaforever.cookieshop.service.CustOrderService;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.javaforever.cookieshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminUserCustRestController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected CustOrderService orderService;
	@Autowired
	protected TypeService typeService;
	@Autowired
	protected UserService userService;
    
    @PostMapping("/admin/user_edit")
    public Map<String,Object> adminUserEdit(@RequestBody UserBean user, Model model) throws Exception{   
    	Map<String,Object> result = new TreeMap<String,Object>();
        UserBean oriUser = userService.findUserById(user.getId());
        if (UserController.tempUser!=null && UserController.tempUser.getPhoto()!=null) {
            oriUser.setPhoto(UserController.tempUser.getPhoto());
        }        
        UserController.tempUser = new  UserBean();
        oriUser.setAddress(user.getAddress());
        oriUser.setPhone(user.getPhone());
        userService.updateUser(oriUser);
        result.put("success",true);
        result.put("data",null);
        return result;
	}    
    
    @GetMapping("/admin/user_reset")
    public Map<String,Object> adminUserReset(@RequestBody UserBean user, Model model) throws Exception{   
    	Map<String,Object> result = new TreeMap<String,Object>();
    	userService.changePasswordUser(user);        
        result.put("success",true);
        result.put("data",null);
        return result;
	}
}
